<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>:r4js</title>
<script type="text/javascript">
var context;
var imgd;
var XRES=640,YRES=480,XI=0,YI=0;
var SYSFRAME;
var SYSFXB;
var SYSKEY=0,SYSXYMOUSE=0,SYSBMOUSE=0;
var SYSALPHA=0xff;SYSINK=0xffffff,SYSPAPER=0;

var dpilab=new ArrayBuffer(1024),dpila=new Int32Array(dpilab);
var rpilab=new ArrayBuffer(1024),rpila=new Int32Array(rpilab);

var progb=new ArrayBuffer(1024*128),prog=new DataView(progb);
var memb=new ArrayBuffer(1024*1024),mem=new DataView(memb);

var MEMLAST,VMEM;
var IP=0,TOS,NOS,RTOS;
var lastbyte=0x98;


document.onmouse=function(e){SYSXYMOUSE=e.x<<16|e.y;}
document.onkeydown=function(e){SYSKEY=e;}
document.onkeyup=function(e){SYSKEY=e;}

function syscls() {
  for (var i=0,n=SYSFRAME.length;i<n;i+=4) {
    SYSFRAME[i]=SYSPAPER&0xff;SYSFRAME[i+1]=(SYSPAPER>>8)&0xff;SYSFRAME[i+2]=(SYSPAPER>>16)&0xff;SYSFRAME[i+3]=255;
  }
}

function systoxfb() { for (var i=0,n=SYSFRAME.length;i<n;i++) { SYSXFB[i]=SYSFRAME[i]; } }
function sysxfbto() { for (var i=0,n=SYSFRAME.length;i<n;i++) { SYSFRAME[i]=SYSXFB[i]; } }
function sysredraw() { context.putImageData(imgd,XI,YI); }

function update()
{
//		SYSPAPER=0xff000;syscls();sysredraw();//break;
//alert("w="+IP);
switch (prog.getUint8(IP++)) {// obtener codigo de ejecucion
case 0: IP=rpila[RTOS];RTOS--;break; //FIN
case 1: NOS++;dpila[NOS]=TOS;TOS=prog.getInt32(IP);IP+=4;break;//LIT
case 2: NOS++;dpila[NOS]=TOS;TOS=mem.getInt32(prog.getInt32(IP));IP+=4;break;//ADR
case 3: RTOS++;rpila[RTOS]=IP+4;IP=prog.getInt32(IP);break;// call
case 4: IP=prog.getInt32(IP);break;//JMP
case 5: IP+=prog.getInt8(IP);break;//JMPR
case 6: W=TOS;TOS=dpila[NOS];NOS--;if (W!=0) { RTOS++;rpila[RTOS]=IP;IP=W; };break;//EXEC
case 7: W=prog.getInt8(IP);IP++;if (TOS!=0) IP+=W; break;//IF
case 8: W=prog.getInt8(IP);IP++;if (TOS<=0) IP+=W; break;//PIF
case 9: W=prog.getInt8(IP);IP++;if (TOS>=0) IP+=W; break;//NIF
case 10:W=prog.getInt8(IP);IP++;if (TOS==0) IP+=W; break;//UIF
case 11:W=prog.getInt8(IP);IP++;if (TOS!=dpila[NOS]) IP+=W;TOS=dpila[NOS];NOS--;break;//IFN
case 12:W=prog.getInt8(IP);IP++;if (TOS==dpila[NOS]) IP+=W;TOS=dpila[NOS];NOS--;break;//IFNO
case 13:W=prog.getInt8(IP);IP++;if (TOS<=dpila[NOS]) IP+=W;TOS=dpila[NOS];NOS--;break;//IFL
case 14:W=prog.getInt8(IP);IP++;if (TOS>=dpila[NOS]) IP+=W;TOS=dpila[NOS];NOS--;break;//IFG
case 15:W=prog.getInt8(IP);IP++;if (TOS<dpila[NOS]) IP+=W;TOS=dpila[NOS];NOS--;break;//IFLE
case 16:W=prog.getInt8(IP);IP++;if (TOS>dpila[NOS]) IP+=W;TOS=dpila[NOS];NOS--;break;//IFGE
case 17:W=prog.getInt8(IP);IP++;if (!(TOS&dpila[NOS])) IP+=W;TOS=dpila[NOS];NOS--;break;//IFAND
case 18:W=prog.getInt8(IP);IP++;if (TOS&dpila[NOS]) IP+=W;TOS=dpila[NOS];NOS--;break;//IFNAND
//--- pila de datos
case 19:NOS++;dpila[NOS]=TOS;break;//DUP
case 20:TOS=dpila[NOS];NOS--;break;//DROP
case 21:NOS++;dpila[NOS]=TOS;TOS=dpila[NOS-1];break;//OVER
case 22:NOS++;dpila[NOS]=TOS;TOS=dpila[NOS-2];break;//PICK2
case 23:NOS++;dpila[NOS]=TOS;TOS=dpila[NOS-3];break;//PICK3
case 24:NOS++;dpila[NOS]=TOS;TOS=dpila[NOS-4];break;//PICK4
case 25:W=dpila[NOS];dpila[NOS]=TOS;TOS=W;break;//SWAP
case 26:NOS--;break; //NIP
case 27:W=TOS;TOS=dpila[NOS-1];dpila[NOS-1]=dpila[NOS];dpila[NOS]=W;break;//ROT
case 28:W=dpila[NOS];NOS++;dpila[NOS]=TOS;NOS++;dpila[NOS]=W;break;//DUP2
case 29:NOS--;TOS=dpila[NOS];NOS--;break;//DROP2
case 30:NOS-=2;TOS=dpila[NOS];NOS--;break;//DROP3
case 31:NOS-=3;TOS=dpila[NOS];NOS--;break;//DROP4
case 32:NOS++;dpila[NOS]=TOS;TOS=dpila[NOS-3];NOS++;dpila[NOS]=TOS;TOS=dpila[NOS-3];break;//OVER2
case 33:W=dpila[NOS];dpila[NOS]=dpila[NOS-2];dpila[NOS-2]=W;W=TOS;TOS=dpila[NOS-1];dpila[NOS-1]=W;break;//SWAP2
case 34:RTOS++;rpila[RTOS]=TOS;TOS=dpila[NOS];NOS--;break;//>r
case 35:NOS++;dpila[NOS]=TOS;TOS=rpila[RTOS];RTOS--;break;//r>
case 36:NOS++;dpila[NOS]=TOS;TOS=rpila[RTOS];break;//ERRE
case 37:rpila[RTOS]+=TOS;TOS=dpila[NOS];NOS--;break;//ERREM:
case 38:NOS++;dpila[NOS]=TOS;TOS=mem[rpila[RTOS]];rpila[RTOS]+=4;break;//ERRFM
case 39:mem[rpila[RTOS]]=TOS;TOS=dpila[NOS];NOS--;rpila[RTOS]+=4;break;//ERRSM
case 40:RTOS--;break;//ERRDR
case 41:TOS&=dpila[NOS];NOS--;break;//AND
case 42:TOS|=dpila[NOS];NOS--;break;//OR
case 43:TOS^=dpila[NOS];NOS--;break;//XOR
case 44:TOS=~TOS;break;//NOT
case 45:TOS=dpila[NOS]+TOS;NOS--;break;//SUMA
case 46:TOS=dpila[NOS]-TOS;NOS--;break;//RESTA
case 47:TOS=dpila[NOS]*TOS;NOS--;break;//MUL
case 48:TOS=dpila[NOS]/TOS;NOS--;break;//DIV
case 49:TOS=(dpila[NOS-1]*dpila[NOS])/TOS;NOS-=2;break;//MULDIV
case 50:TOS=(dpila[NOS-1]*dpila[NOS])>>TOS;NOS-=2;break;//MULSHR
case 51:W=dpila[NOS]%TOS;dpila[NOS]=dpila[NOS]/TOS;TOS=W;break;//DIVMOD
case 52:TOS=dpila[NOS]%TOS;NOS--;break;//MOD
case 53:W=(TOS>>31);TOS=(TOS+W)^W;break;//ABS
case 54:TOS=isqrt32(TOS);break;//CSQRT
case 55:TOS=iclz32(TOS);break;//CLZ
case 56:TOS=(dpila[NOS-1]<<TOS)/dpila[NOS];NOS-=2;break;//CDIVSH
case 57:TOS=-TOS;break;//NEG
case 58:TOS++;break;//INC
case 59:TOS+=4;break;//INC4
case 60:TOS--;break;//DEC
case 61:TOS>>=1;break;//DIV2
case 62:TOS<<=1;break;//MUL2
case 63:TOS=dpila[NOS]<<TOS;NOS--;break;//SHL
case 64:TOS=dpila[NOS]>>TOS;NOS--;break;//SHR
case 65:TOS=mem.getInt32(TOS);break;//FECH
case 66:TOS=mem.getInt8(TOS);break;//CFECH
case 67:TOS=mem.getInt16(TOS);break;//WFECH
case 68:W=dpila[NOS];mem[TOS]=W&0xff;mem[TOS+1]=(W>>8)&0xff;mem[TOS+2]=(W>>16)&0xff;mem[TOS+3]=(W>>24)&0xff;
		NOS--;TOS=dpila[NOS];NOS--;break;//STOR
case 69:mem[TOS]=dpila[NOS];NOS--;TOS=dpila[NOS];NOS--;break;//CSTOR
case 70:W=dpila[NOS];mem[TOS]=W&0xff;mem[TOS+1]=(W>>8)&0xff;NOS--;TOS=dpila[NOS];NOS--;break;//WSTOR
/*
case 71: *((int *)TOS)+=(int)*NOS;NOS--;TOS=dpila[NOS];NOS--;break; //INCSTOR
case 72: mem[TOS]+=dpila[NOS];NOS--;TOS=dpila[NOS];NOS--;break; //CINCSTOR
case WINCSTOR: *((short *)TOS)+=(short)*NOS;NOS--;TOS=*NOS;NOS--;break;
case FECHPLUS: NOS++;*NOS=TOS+4;TOS=*(int *)TOS;break; //@+ | adr -- adr' v
case STOREPLUS: *(int *)TOS=(int)*NOS;TOS+=4;NOS--;break;//!+ | v adr -- adr'
case CFECHPLUS: NOS++;*NOS=TOS+1;TOS=*(char *)TOS;break;
case CSTOREPLUS: *(char *)TOS=(char)*NOS;TOS++;NOS--;break;
case WFECHPLUS: NOS++;*NOS=TOS+2;TOS=*(short *)TOS;break;
case WSTOREPLUS: *(short *)TOS=(short)*NOS;TOS+=2;NOS--;break;
case MOVE:break;
case MOVE>:break;
case CMOVE:break;
case CMOVE>:break;
case MEM:NOS++;dpila[NOS]=TOS;TOS=MEMLAST;break;
case DIR: break;
case FILE: break;
case FSIZE: break;
case VOL: break;
case LOAD: break;
case SAVE: break;
case UPDATE: break;
case XYMOUSE: NOS++;dpila[NOS]=TOS;NOS++;dpila[NOS]=SYSXYM&0xffff;TOS=(SYSXYM>>16);break;
case BMOUSE: NOS++;dpila[NOS]=TOS;TOS=SYSBM;break;
case SKEY: SYSKEY=TOS;TOS=dpila[NOS];NOS--;break;
case KEY:NOS++;dpila[NOS]=TOS;TOS=SYSKEY;break;
case CNTJOY:NOS++;dpila[NOS]=TOS;TOS=0;break;
case GETJOY:NOS++;dpila[NOS]=TOS;TOS=0;break;
case MSEC:NOS++;dpila[NOS]=TOS;TOS=XRES;break;
case TIME:break;
case DATE:break;
case END:return;
case RUN:return;
case SW:NOS++;dpila[NOS]=TOS;TOS=XRES;break;
case SH:NOS++;dpila[NOS]=TOS;TOS=YRES;break;
case CLS:syscls();break;
case REDRAW :sysredraw();break;
case FRAMEV:NOS++;dpila[NOS]=TOS;TOS=0;break;//** no va a andar
case SETXY:VMEM=(dpila[NOS]+(TOS*XRES))<<2;break;
case PX+!:VMEM+=TOS<<2;TOS=dpila[NOS];NOS--;break;
case PX!+:SYSFRAME[VMEM]=TOS&0xff;SYSFRAME[VMEM+1]=(TOS>>8)&0xff;SYSFRAME[VMEM+2]=(TOS>>16)&0xff;SYSFRAME[VMEM+3]=255;
	VMEM+=4;TOS=dpila[NOS];NOS--;break;
case PX@:NOS++;dpila[NOS]=TOS;TOS=SYSFRAME[VMEM]&0xff|(SYSFRAME[VMEM+1]<<8)&0xff00|(SYSFRAME[VMEM+2]<<16)&0xff0000;break;
case XFB:NOS++;dpila[NOS]=TOS;TOS=0;break;//** no va a andar
case >XFB:systoxfb();break;
case XFB>:sysxfbto();break;
case PAPER:SYSPAPER=TOS;TOS=dpila[NOS];NOS--;break;
case INK:SYSINK=TOS;TOS=dpila[NOS];NOS--;break;
case INK@;
case ALPHA:SYSALPHA=TOS;TOS=dpila[NOS];NOS--;break;
case OP:break;
case CP:break;
case LINE:break;
case CURVE:break;
case PLINE:break;
case PCURVE:break;
case POLI:break;
case FCOL:break;
case FCEN:break;
case FMAT:break;
case SFILL:break;
case LFILL:break;
case RFILL:break;
case TFILL:break;
default:
	NOS++;dpila[NOS]=TOS;TOS=W-lastbyte;
*/
case 255:
	SYSPAPER^=0xfff0;syscls();sysredraw();//break;
	TOS--;if (TOS==0) return;
	IP=5;
	break;
	}
setTimeout(update,0);
}

</script></head>
<body>
<canvas id="myCanvas" width="640" height="480">nOTcANVAS
<span style="display:block;width:50px;height:50px;background:#f77">&nbsp;</span>
</canvas>
</body></html>
<script type="text/javascript">
context = document.getElementById('myCanvas').getContext('2d');
if (!context || !context.putImageData) { return; }
if (context.createImageData) { imgd = context.createImageData(XRES,YRES);
  } else if (context.getImageData) { imgd = context.getImageData(0, 0,XRES,YRES);
  } else { imgd = {'width':XRES,'height':YRES,'data':new Array(XRES*YRES*4)}; }

SYSFRAME=imgd.data;
SYSFXB=new Array(XRES*YRES*4);
VMEM=0;
TOS=NOS=RTOS=0;
rpila[0]=56;

IP=0;
prog.setInt8(0,1);
prog.setInt32(1,5);
prog.setInt8(5,-1);
// 5 redraw' 1- 1?goto 6

//syscls();
//sysredraw();
update();
</script>